package com.jihox.abookpro.dao;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jihox.abookpro.domain.BookProduct;
import com.jihox.abookpro.util.DBHelper;
import com.thoughtworks.xstream.XStream;

public class BookProductDAO {

	private String productsFile = null;
	private Context context;
	private DBHelper dbhelper;
	private SQLiteDatabase sqlitedatabase;
	
	public BookProductDAO(Context context) {
		super();
		this.context = context;
	}

	public void opendb() {
		dbhelper = new DBHelper(context);
		sqlitedatabase = dbhelper.getWritableDatabase();
	}

	public void closedb() {
		if (sqlitedatabase != null && sqlitedatabase.isOpen()) {
			sqlitedatabase.close();
		}
	}
	
	//向数据库添加一条BookProduct
	public void addBookProduct(BookProduct bookProduct){
		ContentValues cv = new ContentValues();
		cv.put("ProductId", bookProduct.getPid());
		cv.put("Content", bookProduct.getContent());
		opendb();
		sqlitedatabase.insert("bookproducts", null, cv);
		closedb();
	}
	
	//从数据库删除一条BookProduct
	public void deleteBookProduct(String pid){
		opendb();
		try {
			sqlitedatabase.delete("bookproducts", "ProductId = ? ",
					new String[] { pid });
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closedb();
		}
	}
	//查询所有BookProduct
	public List<BookProduct> getBookProductsList() {
		ArrayList<BookProduct> bookProductsList = new ArrayList<BookProduct>();
		opendb();
		Cursor c = sqlitedatabase.rawQuery("SELECT * FROM bookproducts", null);
		while (c.moveToNext()) {
			BookProduct bookProduct = new BookProduct();
			bookProduct.setPid(c.getString(c.getColumnIndex("ProductId")));
			bookProduct.setContent(c.getString(c.getColumnIndex("Content")));
			bookProductsList.add(bookProduct);
		}
		c.close();
		closedb();
		return bookProductsList;
	}
	//根据BookProductId查询特定的BookProduct
	public BookProduct getBookProduct(String pid) {
		BookProduct bookProduct = new BookProduct();
		opendb();
		Cursor c = sqlitedatabase.rawQuery("SELECT * FROM bookproducts where ProductId = '" + pid +"'", null);
		while (c.moveToNext()) {
			bookProduct.setPid(c.getString(c.getColumnIndex("ProductId")));
			bookProduct.setContent(c.getString(c.getColumnIndex("Content")));
		}
		c.close();
		closedb();
		return bookProduct;
	}
	
	//跟新BookProduct内容
	public void updateBookProductContent(BookProduct bookProduct){
		ContentValues cv = new ContentValues();
		cv.put("Content", bookProduct.getContent());
		opendb();
		sqlitedatabase.update("bookproducts", cv, "ProductId = ? ",
				new String[] { bookProduct.getPid()});
		closedb();
	}
	
	public void save(Vector<BookProduct> products) {
		File file = new File(productsFile);
		XStream xstream = new XStream();
		xstream.alias("BookProducts", Vector.class);
		xstream.alias("BookProduct", BookProduct.class);
		OutputStreamWriter write = null;
		FileOutputStream os = null;
		try {
			os = new FileOutputStream(file);
			write = new OutputStreamWriter(os, "UTF-8");
			xstream.toXML(products, os);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (write != null) {
					write.close();
				}
				if (os != null){
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public Vector<BookProduct> retrieveProducts(InputStream is) throws Exception {
		Vector<BookProduct> bookProducts = new Vector<BookProduct>();

		try {
			XStream xStream = new XStream();
			xStream.alias("BookProducts", Vector.class);
			xStream.alias("BookProduct", BookProduct.class);
			bookProducts = (Vector<BookProduct>) xStream.fromXML(is,
					bookProducts);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
				try {
					if (is != null){
						is.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		return bookProducts;
	}

	//从SD中读取xml文件恢复成BookProduct
	public BookProduct retrieveProduct(InputStream is) throws Exception {
		BookProduct bookProduct = new BookProduct();

		try {
			XStream xStream = new XStream();
			xStream.alias("BookProduct", BookProduct.class);
			bookProduct = (BookProduct) xStream.fromXML(is,
					bookProduct);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
				try {
					if (is != null){
						is.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		return bookProduct;
	}
	
	//从数据库拿到Content恢复成BookProduct
	public BookProduct retrieveProduct(String str) {
		BookProduct bookProduct = new BookProduct();
		try {
			XStream xStream = new XStream();
			xStream.alias("BookProduct", BookProduct.class);
			bookProduct = (BookProduct) xStream.fromXML(str,
					bookProduct);
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		return bookProduct;
	}
	
}
